﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class merchant_addressDal
    {
        /// <summary>
        /// 添加配送地址
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <param name="mercahntid"></param>
        /// <returns></returns>
        public static int Addaddr(string addr, string longitude, string latitude,string mercahntid)
        {
            string sql = "insert into ms_merchant_address(ma_merchant_id,ma_address,ma_create,ma_Longitude,ma_Latitude) values(@ma_merchant_id,@ma_address,@ma_create,@ma_Longitude,@ma_Latitude)";
            MySqlParameter[] values = new MySqlParameter[5];
            values[0] = new MySqlParameter("ma_merchant_id", mercahntid);
            values[1] = new MySqlParameter("ma_address", addr);
            values[2] = new MySqlParameter("ma_create", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[3] = new MySqlParameter("ma_Longitude", longitude);
            values[4] = new MySqlParameter("ma_Latitude", latitude);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 删除配送地址
        /// </summary>
        /// <param name="maid"></param>
        /// <returns></returns>
        public static int Deladdr(string maid)
        {
            string sql = "delete from ms_merchant_address where ma_id=@ma_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("ma_id", maid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查商户配送地址
        /// </summary>
        /// <param name="mercahntid"></param>
        /// <returns></returns>
        public static List<ms_merchant_address> Querybyid(string mercahntid)
        {
            string sql = "select * from ms_merchant_address where ma_merchant_id=@ma_merchant_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("ma_merchant_id", mercahntid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            List<ms_merchant_address> addrlist = new List<ms_merchant_address>();
            while (rd.Read())
            {
                ms_merchant_address addr = new ms_merchant_address();
                addr.ma_id = rd["ma_id"].ToString();
                addr.ma_merchant_id = rd["ma_merchant_id"].ToString();
                addr.ma_address = rd["ma_address"].ToString();
                addr.ma_create = rd["ma_create"].ToString();
                addr.ma_Longitude = rd["ma_Longitude"].ToString();
                addr.ma_Latitude = rd["ma_Latitude"].ToString();
                addrlist.Add(addr);
            }
            rd.Close();
            return addrlist;

        }
    }
}
